package bank;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static bank.TransactionType.*;

public class Test {
    public static void main(String[] args) {
        // 初始化账户和交易记录集合
        Map<String, Account> accounts = new HashMap<>();
        List<TransactionRecord> transactionRecords = new ArrayList<>();

        // 创建测试账户并添加到集合
        Account account1 = new Account("ACC001", "Alice", 1000.0);
        Account account2 = new Account("ACC002", "Bob", 500.0);
        accounts.put(account1.getAccountNumber(), account1);
        accounts.put(account2.getAccountNumber(), account2);

        BankService bankService = new BankService(accounts, transactionRecords);

        // 测试交易执行
        testExecuteTransaction(bankService);

        // 测试审计日志导出
        testExportAuditLog(bankService);
    }

    private static void testExecuteTransaction(BankService bankService) {
        System.out.println("=== 测试交易执行 ===");

        // 创建存款交易
        LocalDateTime depositTime = LocalDateTime.of(2024, 5, 1, 10, 30);
        TransactionRecord depositRecord = new TransactionRecord(1, depositTime, "ACC001", DEPOSIT,
                500.0, null, 1500.0);
        bankService.executeTransaction(depositRecord);

        // 创建转账出交易
        LocalDateTime transferOutTime = LocalDateTime.of(2024, 5, 2, 14, 15);
        TransactionRecord transferOutRecord = new TransactionRecord(
                2, transferOutTime, "ACC001", TRANSFER_OUT,
                300.0, "ACC002", 1200.0
        );
        bankService.executeTransaction(transferOutRecord);

        // 创建转入交易
        LocalDateTime transferInTime = LocalDateTime.of(2024, 5, 2, 14, 16);
        TransactionRecord transferInRecord = new TransactionRecord(
                3, transferInTime, "ACC002",TRANSFER_IN,
                300.0, "ACC001", 800.0
        );
        bankService.executeTransaction(transferInRecord);

        // 打印交易后余额
        System.out.println("ACC001 余额: " + bankService.accounts.get("ACC001").getBalance()); // 预期1200.0
        System.out.println("ACC002 余额: " + bankService.accounts.get("ACC002").getBalance()); // 预期800.0
        System.out.println("交易记录数: " + bankService.transactionRecords.size()); // 预期3
    }

    private static void testExportAuditLog(BankService bankService) {
        System.out.println("\n=== 测试审计日志导出 ===");

        // 定义时间范围（2024年5月1日至2024年5月3日）
        LocalDateTime start = LocalDateTime.of(2024, 5, 1, 0, 0);
        LocalDateTime end = LocalDateTime.of(2024, 5, 3, 23, 59);

        // 导出ACC001的交易记录到指定路径（注意：请根据实际环境修改文件路径）
        bankService.exportAuditLog("ACC001", start, end, "D:\\a\\c.csv");
    }
}
